Completed
Push — master ( d1844c...194c7c )
by
unknown
03:08
created

page.js ➔ page   C

Complexity

Conditions 21
Paths 60

Size

Total Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
nc 60
dl 0
loc 78
rs 5.1035
cc 21
nop 3

2 Functions

Rating   Name   Duplication   Size   Complexity  
A page.js ➔ ... ➔ ??? 0 5 1
A page.js ➔ ... ➔ cmsData.source.catch 0 3 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like page.js ➔ page often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import mkdirp from 'mkdirp'
2
import {
3
  Util,
0 ignored issues
show
Unused Code introduced by
The variable Util seems to be never used. Consider removing it.
Loading history...
4
  cmsData,
5
  FileParser,
6
  fileUtils,
7
  config,
8
  Page,
9
  cmsTemplate,
10
  cleanSlug
11
} from '../../cli'
12
13
var page = function (req, res, next) {
0 ignored issues
show
Unused Code introduced by
The parameter next is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
14
  var filePath = cleanSlug(req.query.filePath)
15
  filePath = fileUtils.getFilePath(filePath)
16
  var html = (req.query.html) ? true : false
17
  var json = null
18
  var editor = false
19
  if(typeof req.body.json !== 'undefined' && req.body.json !== null) {
20
    editor = true
21
    if(typeof req.body.json === 'string') {
22
      json = JSON.parse(req.body.json)
23
    }else {
24
      json = req.body.json
25
    }
26
  }
27
28
  if(typeof filePath !== 'undefined' && filePath !== null) {
29
    var jsonPath = null
30
    var linkPath = null
31
32
    var filePathTest = cmsData.revision.getDocumentRevision(req.query.filePath)
33
    if(typeof filePathTest !== 'undefined' && filePathTest !== null) {
34
      // filePath = filePathTest.path
35
      jsonPath = filePathTest.path
36
      linkPath = filePathTest.abe_meta.link
37
    }
38
39
    if(jsonPath === null || !fileUtils.isFile(jsonPath)) { 
40
      res.status(404).send('Not found')
41
      return
42
    } 
43
44
    if(typeof json === 'undefined' || json === null) {
45
      json = FileParser.getJson(jsonPath)
46
    }
47
    
48
    let meta = config.meta.name
49
50
    var template = ''
51
    if(typeof json[meta] !== 'undefined' && json[meta] !== null && json[meta] !== ''
52
      && json[meta].template !== 'undefined' && json[meta].template !== null && json[meta].template !== '') {
53
      template = json[meta].template
54
    }else {
55
      template = req.params[0]
56
    }
57
    var text = cmsTemplate.template.getTemplate(template)
58
59
    if (!editor) {
60
61
      cmsData.source.getDataList(fileUtils.removeLast(linkPath), text, json)
62
        .then(() => {
63
          var page = new Page(template, text, json, html)
64
          res.set('Content-Type', 'text/html')
65
          res.send(page.html)
66
        }).catch(function(e) {
67
          console.error(e)
68
        })
69
    }else {
70
      text = cmsData.source.removeDataList(text)
71
      var page = new Page(template, text, json, html)
72
      res.set('Content-Type', 'text/html')
73
      res.send(page.html)
74
    }
75
  }else {
76
    // not 404 page if tag abe image upload into each block
77
    if(/upload%20image/g.test(req.url)) {
78
      var b64str = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
79
      var img = new Buffer(b64str, 'base64')
0 ignored issues
show
Bug introduced by
The variable Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
80
81
      res.writeHead(200, {
82
        'Content-Type': 'image/jpeg',
83
        'Content-Length': img.length
84
      })
85
      res.end(img) 
86
    }else {
87
      res.status(404).send('Not found')
88
    }
89
  }
90
}
91
92
export default page